package net.yla.smartbus.module.ba.area.serice.impl;

import com.github.pagehelper.PageHelper;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.module.ba.area.detachment.AreaDetachment;
import net.yla.smartbus.module.ba.area.dto.AreaDto;
import net.yla.smartbus.module.ba.area.entity.BaRegionsDo;
import net.yla.smartbus.module.ba.area.form.AreaForm;
import net.yla.smartbus.module.ba.area.mapper.AreaMapper;
import net.yla.smartbus.module.ba.area.query.AreaQuery;
import net.yla.smartbus.module.ba.area.serice.AreaService;
import net.yla.smartbus.module.ba.area.service.PointsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 默苍离
 * @date 2021/6/26 10:17
 * <p>
 * ******************************************
 * @annotation:
 */
@Service
public class AreaServiceImpl implements AreaService {

    private final AreaDetachment areaDetachment;
    private final AreaMapper areaMapper;
    private final PointsService pointsService;

    public AreaServiceImpl(AreaMapper areaMapper, PointsService pointsService, AreaDetachment areaDetachment) {
        this.areaMapper = areaMapper;
        this.pointsService = pointsService;
        this.areaDetachment = areaDetachment;
    }


    /**
     * 获取所有区域
     *
     * @param query
     * @return
     */
    @Override
    public List<AreaDto> getAreas(AreaQuery query) {

        List<BaRegionsDo> areas = areaMapper.getAreas(query);
        ArrayList<AreaDto> areaList = new ArrayList<>();

        for (int i = 0; i < areas.size(); i++) {
            BaRegionsDo area = areas.get(i);
            AreaDto areaDto = areaDetachment.areaDoToTto(area);
            areaList.add(areaDto);
        }

        return areaList;
    }


    /**
     * 根据id查询一条区域数据
     *
     * @param regionId
     * @return
     */
    @Override
    public AreaDto getOneArea(Integer regionId) {
        BaRegionsDo oneArea = areaMapper.getOneArea(regionId);
        if (oneArea != null) {
            AreaDto areaDto = areaDetachment.areaDoToTto(oneArea);


            return areaDto;
        }
        return null;
    }


    /**
     * 获取所有区域数量
     *
     * @param query
     * @return
     */
    @Override
    public Integer getAreasCount(AreaQuery query) {
        return areaMapper.getAreasCount(query);
    }


    /**
     * 分页获取所有区域
     *
     * @param query
     * @return
     */
    @Override
    public PageBean<AreaDto> getAreasByPage(AreaQuery query) {
        Integer areasCount = this.getAreasCount(query);
        PageHelper.startPage(query.getPageNum(), query.getPageSize(), "lastModifyTime desc");
        List<AreaDto> areas = this.getAreas(query);
        PageBean<AreaDto> pageBean = new PageBean<>(query.getPageNum(), query.getPageSize(), areasCount, areas);
        return pageBean;
    }


    /**
     * 根据区域id进行删除
     *
     * @param ids 区域信息表 主键 id 集合
     * @return
     */
    @Override
    public Integer deleteArea(Integer[] ids) {
        Integer integer = areaMapper.deleteArea(ids);
        return integer;
    }


    /**
     * 添加区域
     *
     * @param form
     * @return
     */
    @Override
    public Integer addArea(AreaForm form) {
        Integer integer = areaMapper.addArea(form);
        return integer;
    }


    /**
     * 更新区域
     *
     * @param form
     * @return
     */
    @Override
    public Integer updateArea(AreaForm form) {
        Integer integer = areaMapper.updateArea(form);
        return integer;
    }


}
